package demo2;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Test {

    public static void reflectPrivateConstructor() {
        try {
            Class<?> classStudent = Class.forName("demo2.TestEnum");
            // 此处会报错： demo2.TestEnum.<init>(java.lang.String,int)
            Constructor<?> constructor=classStudent.getDeclaredConstructor(String.class,int.class,String.class,int.class);

            constructor.setAccessible(true);

            TestEnum testEnum=(TestEnum)constructor.newInstance("gaobo",29);

            System.out.println(testEnum);

        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
        //Cannot reflectively create enum objects  ：修改之后还是会报错，理由是枚举类型不能通过反射来修改
    }
}
